package com.chuangjiangx.member.basic.web.config;


import com.chuangjiangx.member.stored.ddd.domain.subscribe.MbrOldOrderPaySuccessListener;
import com.chuangjiangx.member.stored.ddd.domain.subscribe.MbrOldOrderRefundSuccessListener;
import com.chuangjiangx.member.stored.ddd.domain.subscribe.MbrPaySuccessListener;
import com.chuangjiangx.member.stored.ddd.domain.subscribe.MbrRefundSuccessListener;
import com.chuangjiangx.member.basic.web.websocket.MbrClaimCodeUseListener;
import com.chuangjiangx.member.basic.web.websocket.MemberCodePayListener;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author zhangquanquan on 2018/7/2.
 */
public class RedisConfig {

    @Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private int port;
    @Value("${redis.password}")
    private String password;
    @Value("${redis.maxIdle}")
    private int maxIdle;
    @Value("${redis.minIdle}")
    private int minIdle;
    @Value("${redis.maxTotal}")
    private int maxTotal;
    @Value("${redis.testOnBorrow}")
    private boolean testOnBorrow;

    /**
     * 配置default ConnectionFactory jdkSerializer
     * @return
     */
    @Bean
    public StringRedisSerializer jdkSerializer(){
        return new StringRedisSerializer();
    }

    /**
     * string模版，兼容以前代码,string模版以后统一使用stringRedisTemplate
     */
    @Bean(name = "redisTemplate")
    public StringRedisTemplate redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(factory);
        return stringRedisTemplate;
    }

    /**
     * string模版
     */
    @Bean(name = "stringRedisTemplate")
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(factory);
        return stringRedisTemplate;
    }

    /**
     * json模版
     */
    @Bean(name = "jsonRedisTemplate")
    public RedisTemplate<Object, Object> jsonRedisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值（默认使用JDK的序列化方式）
        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(mapper);
        template.setValueSerializer(serializer);
        //使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

    /**
     * redis连接的基础设置
     */
    @Bean
    public JedisConnectionFactory redisConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setHostName(host);
        factory.setPort(port);
        factory.setPassword(password);
        factory.setUsePool(true);
        factory.setPoolConfig(jedisPoolConfig());
        return factory;
    }

    /**
     * 连接池配置
     */
    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMinIdle(minIdle);
        jedisPoolConfig.setMaxTotal(maxTotal);
        jedisPoolConfig.setTestOnBorrow(testOnBorrow);
        return jedisPoolConfig;
    }

    /**
     * 配置redis消息订阅（原xml）
     */
    @Bean
    public RedisMessageListenerContainer container(
            RedisConnectionFactory connectionFactory,
            MemberCodePayListener memberCodePayListener,
            MbrClaimCodeUseListener mbrClaimCodeUseListener,
            MbrPaySuccessListener mbrPaySuccessListener,
            MbrRefundSuccessListener mbrRefundSuccessListener,
            MbrOldOrderPaySuccessListener mbrOldOrderPaySuccessListener,
            MbrOldOrderRefundSuccessListener mbrOldOrderRefundSuccessListener
            ){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 会员码付款监听
        MessageListenerAdapter memberCodePay = new MessageListenerAdapter(memberCodePayListener);
        memberCodePay.afterPropertiesSet();
        container.addMessageListener(memberCodePay, new PatternTopic("stored-scan-pay"));
        // 取货码核销监听
        MessageListenerAdapter mbrClaimCodeUse = new MessageListenerAdapter(mbrClaimCodeUseListener);
        mbrClaimCodeUse.afterPropertiesSet();
        container.addMessageListener(mbrClaimCodeUse, new PatternTopic("claimcode_use_success"));

        // 会员支付监听
        MessageListenerAdapter mbrPaySuccess = new MessageListenerAdapter(mbrPaySuccessListener);
        mbrPaySuccess.afterPropertiesSet();
        container.addMessageListener(mbrPaySuccess, new PatternTopic("mbr_pay_success"));
        MessageListenerAdapter mbrRefundSuccess = new MessageListenerAdapter(mbrRefundSuccessListener);
        mbrRefundSuccess.afterPropertiesSet();
        container.addMessageListener(mbrRefundSuccess, new PatternTopic("mbr_refund_success"));

        // 移动支付监听
        MessageListenerAdapter mbrOldOrderPaySuccess = new MessageListenerAdapter(mbrOldOrderPaySuccessListener);
        mbrOldOrderPaySuccess.afterPropertiesSet();
        container.addMessageListener(mbrOldOrderPaySuccess, new PatternTopic("order_pay_success"));
        MessageListenerAdapter mbrOldOrderRefundSuccess = new MessageListenerAdapter(mbrOldOrderRefundSuccessListener);
        mbrOldOrderRefundSuccess.afterPropertiesSet();
        container.addMessageListener(mbrOldOrderRefundSuccess, new PatternTopic("order_refund_success"));
        return container;
    }


    @Bean
    public MemberCodePayListener memberCodePayListener(){
        return new MemberCodePayListener();
    }
    @Bean
    public MbrClaimCodeUseListener mbrClaimCodeUseListener(){
        return new MbrClaimCodeUseListener();
    }
    @Bean
    public MbrPaySuccessListener mbrPaySuccessListener(){
        return new MbrPaySuccessListener();
    }
    @Bean
    public MbrRefundSuccessListener mbrRefundSuccessListener(){
        return  new MbrRefundSuccessListener();
    }
    @Bean
    public MbrOldOrderPaySuccessListener mbrOldOrderPaySuccessListener(){
        return new MbrOldOrderPaySuccessListener();
    }
    @Bean
    public MbrOldOrderRefundSuccessListener mbrOldOrderRefundSuccessListener(){
        return new MbrOldOrderRefundSuccessListener();
    }





}
